import { ref } from 'vue';

// 放大、缩小
export function useScale(imageScale = { max: 2, min: 0.5, step: 0.5 }) {
  const { max, min, step } = imageScale;
  const scale = ref(1);
  const onZoomIn = () => {
    setScale(scale.value + step);
  };
  const onZoomOut = () => {
    setScale(scale.value - step);
  };
  const resetScale = () => {
    scale.value = 1;
  };

  const setScale = newScale => {
    let value = newScale;
    if (newScale < min) {
      value = min;
    }
    if (newScale > max) {
      value = max;
    }
    scale.value = value;
  };

  return { scale, onZoomIn, onZoomOut, resetScale };
}

// 旋转
export function useRotate() {
  const rotate = ref(0);
  const ROTATE_DEG = 90;

  const onRotate = type => {
    if (type === 'right') rotate.value += ROTATE_DEG;
    if (type === 'left') rotate.value -= ROTATE_DEG;
  };
  const resetRotate = () => {
    rotate.value = 0;
  };

  return { rotate, onRotate, resetRotate };
}
